﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using System.ComponentModel;
using System.Drawing;
using XCI.Core;
using XCI.Helper;

namespace XCI.Windows
{
    /// <summary>
    /// 数据字典单选控件
    /// </summary>
    public class DicRadioGroupx : RadioGroupx
    {
        static DicRadioGroupx()
        {
            RepositoryItemDicRadioGroupx.RegisterDicRadioGroupx();
        }

        public DicRadioGroupx()
        {
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string ValueMember => "Value";
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string DisplayMember => "Name";

        /// <summary>
        /// 字典编码
        /// </summary>
        [Category("XCI")]
        [Description("字典编码")]
        [DefaultValue(null)]
        public string DicCode { get; set; }
         
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;
            if (string.IsNullOrEmpty(DicCode)) return;

            LoadDic(DicCode);
        }

        public void LoadDic(string code)
        {
            var dic = App.Handler.GetDic(code);
            DataSource = dic.Items;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemDicRadioGroupx Properties => base.Properties as RepositoryItemDicRadioGroupx;

        public override string EditorTypeName => RepositoryItemDicRadioGroupx.CustomEditName;
    }

    [UserRepositoryItem("RegisterDicRadioGroupx")]
    public class RepositoryItemDicRadioGroupx : RepositoryItemRadioGroupx
    {
        static RepositoryItemDicRadioGroupx()
        {
            RegisterDicRadioGroupx();
        }

        public new const string CustomEditName = "DicRadioGroupx";

        public RepositoryItemDicRadioGroupx()
        {
        }

        public override string EditorTypeName => CustomEditName;

        public static void RegisterDicRadioGroupx()
        {
            Image img = null;
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName,
                typeof(DicRadioGroupx), typeof(RepositoryItemDicRadioGroupx),
                typeof(RadioGroupxViewInfo), new RadioGroupxPainter(), true, img));
        }

        public override void Assign(RepositoryItem item)
        {
            BeginUpdate();
            try
            {
                base.Assign(item);
                if (!(item is RepositoryItemDicRadioGroupx source)) return;
                //
            }
            finally
            {
                EndUpdate();
            }
        }
    }
}